package com.pkk.cloud.support.admin.controller.interior.user;

import com.github.pagehelper.PageInfo;
import com.pkk.cloud.support.admin.dto.req.user.OauthBindUserClientReqDto;
import com.pkk.cloud.support.admin.dto.req.user.OauthUserAddReqDto;
import com.pkk.cloud.support.admin.dto.req.user.OauthUserDelReqDto;
import com.pkk.cloud.support.admin.dto.req.user.OauthUserIdReqDto;
import com.pkk.cloud.support.admin.dto.req.user.OauthUserQueryPageReqDto;
import com.pkk.cloud.support.admin.dto.req.user.OauthUserUpdateReqDto;
import com.pkk.cloud.support.admin.dto.resp.user.OauthSysUserDetailRespDto;
import com.pkk.cloud.support.admin.dto.resp.user.OauthSysUserQueryPageRespDto;
import com.pkk.cloud.support.admin.dto.resp.user.OauthUserClientRespDto;
import com.pkk.cloud.support.admin.service.PenguinSysUserService;
import com.pkk.cloud.support.oauth2.client.aspect.annotation.PreAuth;
import com.pkk.cloud.support.oauth2.common.utils.SecurityUtils;
import com.pkk.components.rpc.request.CommonRequest;
import com.pkk.components.rpc.request.PagedQueryRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 用户管理控制器
 * @author: peikunkun
 * @create: 2020-04-01 21:54
 **/
@Slf4j
@RestController
@RequestMapping("/oauth/user")
public class OauthUserController {

  @Resource
  private PenguinSysUserService penguinSysUserService;

  /**
   * @Description: 添加用户
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:57
   */
  @PreAuth
  @PostMapping("add")
  public CommonResponse<Boolean> add(@Validated @RequestBody CommonRequest<OauthUserAddReqDto> request) {
    final String userId = SecurityUtils.getUserId();
    return ResponseUtil.success(penguinSysUserService.saveSysUser(request.getBody(), userId));
  }


  /**
   * @Description: 删除用户操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("del")
  public CommonResponse<Boolean> del(@Validated @RequestBody CommonRequest<OauthUserDelReqDto> request) {
    return ResponseUtil.success(penguinSysUserService.delSysUserById(request.getBody().getId()));
  }

  /**
   * @Description: 更新用户操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("update")
  public CommonResponse<Boolean> update(@Validated @RequestBody CommonRequest<OauthUserUpdateReqDto> request) {
    return ResponseUtil.success(penguinSysUserService.updateSysUserById(request.getBody()));
  }


  /**
   * @Description: 获取用户列表操作
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/1 0001 下午 8:59
   */
  @PreAuth
  @PostMapping("list")
  public CommonResponse<PageInfo<OauthSysUserQueryPageRespDto>> list(
      @RequestBody PagedQueryRequest<OauthUserQueryPageReqDto> request) {
    return ResponseUtil.success(penguinSysUserService.findSysUserPageList(request.getPage(), request.getBody()));
  }


  /**
   * @Description: 获取用户信息依据主键id
   * @Param: request
   * @Author: peikunkun
   * @Date: 2020/4/6 0006 上午 10:13
   */
  @PreAuth
  @PostMapping("findUserById")
  public CommonResponse<OauthSysUserDetailRespDto> findUserById(
      @Validated @RequestBody CommonRequest<OauthUserIdReqDto> request) {
    return ResponseUtil.success(penguinSysUserService.findUserById(request.getBody()));
  }


  /**
   * @Description: 获取客户端的绑定关系依据用户id
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<com.pkk.cloud.support.admin.dto.resp.user.OauthUserClientRespDto>
   * @Author: peikunkun
   * @Date: 2020/4/8 下午9:43
   */
  @PreAuth
  @PostMapping("findUserClientByUserId")
  public CommonResponse<OauthUserClientRespDto> findUserClientByUserId(
      @Validated @RequestBody CommonRequest<OauthUserIdReqDto> request) {
    return ResponseUtil.success(penguinSysUserService.findUserClientByUserId(request.getBody()));
  }


  /**
   * @Description: 绑定用户与访问客户端的信息
   * @Param: request
   * @return: com.pkk.components.rpc.response.CommonResponse<java.lang.Boolean>
   * @Author: peikunkun
   * @Date: 2020/4/8 下午10:05
   */
  @PreAuth
  @PostMapping("bindUserClient")
  public CommonResponse<Boolean> bindUserClient(
      @Validated @RequestBody CommonRequest<OauthBindUserClientReqDto> request) {
    return ResponseUtil.success(penguinSysUserService.bindUserClient(request.getBody()));
  }


}
